package com.zhou;

import java.util.*;

/**
 * TT
 *
 * @author zhouxianfeng
 * @date 2021-8-18
 */


public class TT {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int pmCount = sc.nextInt();
        int softCount = sc.nextInt();
        int ideaCount = sc.nextInt();
        sc.nextLine();

        List<Idea> ideas = new ArrayList<>();
        for (int i = 1; i <= ideaCount; i++) {
            Idea idea = new Idea();
            idea.id = i;
            idea.pmno = sc.nextInt();
            idea.submitTime = sc.nextInt();
            idea.order = sc.nextInt();
            idea.needTime = sc.nextInt();
            ideas.add(idea);
            sc.nextLine();
        }
        Map<Integer, Idea> completed = new HashMap<>();
        work(ideas, softCount, completed, ideaCount, pmCount);
        for (int i = 1; i <= ideaCount; i++) {
            System.out.println(completed.get(i).endTime);
        }
    }

    public static void sortForPM(List<Idea> ideas) {
        ideas.sort((i1, i2) -> {

            int seq1 = i2.order - i1.order;
            int seq2 = i1.needTime - i2.needTime;
            int seq3 = i1.submitTime - i2.submitTime;
            return seq1 != 0 ? seq1 : (seq2 != 0 ? seq2 : seq3);

        });
    }

    public static void selectBySubmitTime(Map<Integer, List<Idea>> pmIdeaMap,
                                          List<Idea> ideaList, int currTime) {

        for (Idea idea : ideaList) {
            if (idea.submitTime == currTime) {
                pmIdeaMap.get(idea.pmno).add(idea);
            }
        }
        for (int i = 1; i <= pmIdeaMap.size(); i++) {
            if (!pmIdeaMap.get(i).isEmpty()) {
                sortForPM(pmIdeaMap.get(i));
            }
        }
    }

    public static void sortForSoft(List<Idea> ideas) {
        ideas.sort((i1, i2) -> {
            int seq1 = i1.needTime - i2.needTime;
            int seq2 = i1.pmno - i2.pmno;
            return seq1 != 0 ? seq1 : seq2;
        });
    }

    public static void work(List<Idea> ideaList, int softCount,
                            Map<Integer, Idea> completed, int ideaCount, int pmCount) {
        Map<Integer, List<Idea>> pmIdeaMap = new HashMap<>();
        for (int j = 1; j <= pmCount; j++) {
            pmIdeaMap.putIfAbsent(j, new ArrayList<>());
        }

        int curTime = 0;
        List<Idea> working = new ArrayList<>();
        while (true) {
            if (completed.size() == ideaCount) {
                return;
            }
            curTime++;

            // add for pm
            selectBySubmitTime(pmIdeaMap, ideaList, curTime);
            // add time
            List<Idea> toDelete = new ArrayList<>();
            for (Idea idea : working) {
                if (curTime == idea.endTime) {
                    toDelete.add(idea);
                    completed.put(idea.id, idea);
                    softCount++;
                }
            }
            working.removeAll(toDelete);
            while (softCount > 0) {
                Idea idea = selectForSoft(pmIdeaMap);
                if (idea == null) {
                    break;
                }
                idea.endTime = curTime + idea.needTime;
                softCount--;
                working.add(idea);
            }

        }

    }

    public static Idea selectForSoft(Map<Integer, List<Idea>> pmIdeaMap) {
        List<Idea> toWorkSet = new ArrayList<>();
        for (List<Idea> itemList : pmIdeaMap.values()) {
            if (itemList.size() > 0) {
                toWorkSet.add(itemList.get(0));
            }
        }

        if (!toWorkSet.isEmpty()) {
            sortForSoft(toWorkSet);
            Idea idea = toWorkSet.get(0);
            pmIdeaMap.get(idea.pmno).remove(idea);
            return idea;
        }

        return null;
    }

    static class Idea {

        public int id;
        public int pmno;
        public int submitTime;
        public int order;
        public int needTime;
        public int endTime;
    }


}
